package com.kanqiubao.kq.activity.user;

import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.kanqiubao.kq.R;
import com.kanqiubao.kq.activity.BaseActivity;
import com.kanqiubao.kq.utils.SystemIntentUtils;
import com.maple.msdialog.ActionSheetDialog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;

/**
 * 用户信息
 */
public class UserCenterActvity extends BaseActivity<String> {
    private ImageView mImageView;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        mImageView = findView(R.id.face);
    }

    @Override
    protected int getActivitylayout() {
        return R.layout.user_center_layout;
    }

    @Override
    protected String getTitleText() {
        return "我";
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.sex_click_id:
                sexDialog();
                break;
            case R.id.pz_click_id:
                pzDialog();
                break;
            case R.id.time_click_id:
                showTime();
                break;
            case R.id.nick_click_id:
                startMyActivity(UserChangeNickActivity.class);
                break;
            case R.id.tel_click_id:
                startMyActivity(UserChangeTelActivity.class);
                break;
            case R.id.pwd_click_id:
                startMyActivity(UserChangePwdActivity.class);
                break;
        }
        super.click(v);
    }

    private void sexDialog() {
        new ActionSheetDialog(this)
                .setCancelable(false)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("男",
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                            }
                        })
                .addSheetItem("女",
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                            }
                        })
                .setCancelText("取 消")
                .show();
    }

    private void pzDialog() {
        new ActionSheetDialog(this)
                .setCancelable(false)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("拍照",
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                SystemIntentUtils.openCamera(UserCenterActvity.this, new File(getExternalCacheDir().getAbsolutePath() + File.separator + "camera.jpg"));
                            }
                        })
                .addSheetItem("相册",
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                SystemIntentUtils.openPhoto(UserCenterActvity.this);
                            }
                        })
                .setCancelText("取 消")
                .show();
    }

    private void showTime() {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
            }
        }).setType(new boolean[]{true, true, true, false, false, false}).build();
        pvTime.show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO 自动生成的方法存根
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case SystemIntentUtils.SystemRequestCodePhoto:
                //相册
                SystemIntentUtils.startPhotoCrop(data.getData(), new File(getCacheDir().getAbsolutePath() + File.separator + "face" + ".jpg")
                        , 400, 400, this);
                break;
            case SystemIntentUtils.SystemRequestCodeCamera:
                //拍照
                SystemIntentUtils.startPhotoCrop(SystemIntentUtils.getPathToUri( this,getExternalCacheDir().getAbsolutePath() + File.separator + "camera.jpg"), new File(getCacheDir().getAbsolutePath() + File.separator + "face" + ".jpg")
                        , 400, 400, this);
                break;
            case SystemIntentUtils.SystemRequestCodeCrop:
                try {
                    Bitmap photo = BitmapFactory.decodeStream(new FileInputStream(new File(getCacheDir().getAbsolutePath() + File.separator + "face" + ".jpg")));
                    mImageView.setImageBitmap(photo);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                break;
        }
//        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
//            Uri selectedImage = data.getData();
//            String[] filePathColumn = {MediaStore.Images.Media.DATA};
//            //查询我们需要的数据
//            Cursor cursor = getContentResolver().query(selectedImage,
//                    filePathColumn, null, null, null);
//            cursor.moveToFirst();
//
//            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//            String picturePath = cursor.getString(columnIndex);
//            cursor.close();
//            //拿到了图片的路径picturePath可以自行使用
//            SystemIntentUtils.startPhotoCrop(selectedImage, new File(getCacheDir().getAbsolutePath() + File.separator + "face.jpg")
//                    , 400, 400, this);
////            img_view.setImageBitmap(BitmapFactory.decodeFile(picturePath));

//        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
